package com.young.shop.api.controller;


import com.young.shop.api.service.MyCartsService;
import com.young.shop.pojo.qo.CartsInfoQO;
import com.young.shop.pojo.vo.BaseRespVO;
import com.young.shop.pojo.vo.MyShopCartsVO;
import com.young.shop.pojo.vo.PayVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "数据中心业务")
@RestController
@RequestMapping("/myCart")
public class MyCartsController extends BaseController {

    @Autowired
    private MyCartsService myCartsService;

    @ApiOperation("获取订单详情")
    @PostMapping("/insertGoods")
    public void addMyCart(int goodsId, int userId, int goodsNumber){

        //调用service
        myCartsService.insertGoods(goodsId,userId,goodsNumber);

    }

    @ApiOperation("初始化购物车")
    @PostMapping("/initCart")
    public BaseRespVO initCart(int userId){

        System.out.println(userId);
        //调用service
        try {
            List<MyShopCartsVO> myShopCartsVOS = myCartsService.initCarts(userId);
            return convertSuccessResult(myShopCartsVOS);
        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }
    @ApiOperation("生成订单")
    @PostMapping("/creatOrder")
    public BaseRespVO addOrder(@RequestBody CartsInfoQO[] cartsList){
        System.out.println("进入生成订单");
        System.out.println(cartsList[0]);

        try {
            Long id = myCartsService.addOrder(cartsList);
            return convertSuccessResult(id);
        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }

    }

    @ApiOperation("移除购物车商品")
    @PostMapping("/removeCarts")
    public void removeCarts(int id){
        System.out.println("进入删除订单");
        myCartsService.removeOrder(id);
    }

    @ApiOperation("初始化支付页面")
    @PostMapping("/initPay")
    public BaseRespVO initPay(int orderId){
        System.out.println("进入初始支付页面");
        System.out.println(orderId);
        try {
            List<PayVO> payVOS = myCartsService.initPay(orderId);
            return convertSuccessResult(payVOS);
        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }
}
